import CssAttrConst from "./CssAttrConst";
import * as FS from "fs";
import StrNameValBO from "./StrNameValBO";

export default class JsCssGenerate {
  static generate() {
    for (const cssAttr of CssAttrConst.attrs) {
      const attrName = this.lineToHump(cssAttr.attrName);
      const capitalizeAttrName = this.capitalizeAttrName(attrName)
      let attrCssStr = this.generateClassName(capitalizeAttrName)

      if (cssAttr.isNumber) {
        attrCssStr += this.generateNumberMethodName(attrName)
      } else {
        for (const attrValue of cssAttr.attrValues) {
          attrCssStr += this.generateMethodName(attrName, this.disposeValueName(attrValue))
        }
      }
      attrCssStr += '}'
      const filePath = `../cssModule/singleStyle/${capitalizeAttrName}.ts`
      console.log(filePath)
      FS.writeFile(filePath, attrCssStr, () => {
      })
    }
  }

  static generateClassName(attrName: string) {
    return `export default class ${attrName.charAt(0).toUpperCase() + attrName.slice(1)} {\n`
  }

  static generateMethodName(attrName: string, value: string) {
    return `static get ${value}(){
      return {
        ${attrName}: '${value}'
      }
    }\n`
  }


  static generateNumberMethodName(attrName: string) {
    const numberUnits: StrNameValBO[] = [new StrNameValBO('', 'px'), new StrNameValBO('p', '%'), new StrNameValBO('vw'), new StrNameValBO('vh')]
    const firstWord = attrName.charAt(0)
    let cssStr = `static get ${firstWord}0(){
      return {
        ${attrName}: 0
      }
    }\n`

    for (const numberUnit of numberUnits) {
      for (let i = 1; i < 101; i++) {
        cssStr += `static get ${firstWord + i + numberUnit.name}(){
          return {
            ${attrName}: '${i + numberUnit.val}'
          }
        }\n`
      }
    }

    return cssStr
  }

  static disposeValueName(value) {
    if (this.isNumber(value)) {
      return 'a' + value
    }
    return this.lineToHump(value)
  }

  static isNumber(obj) {
    // 等于number，或者有值且不为NaN
    return typeof obj === 'number' || (!!obj && !isNaN(obj))
  }

  static lineToHump(val: string) {
    if (val) {
      const valAry = val.split('-')
      if (valAry.length > 1) {
        return valAry.map((item, index) => index > 0 ? this.capitalizeAttrName(item) : item).join('')
      }
    }
    return val
  }

  static capitalizeAttrName(val: string) {
    return val.charAt(0).toUpperCase() + val.slice(1)
  }
}


JsCssGenerate.generate()

